package com.schrodinger.zbdp.licensegen.frame.util;

import java.io.File;

import javax.swing.filechooser.FileFilter;

public class GenericFile extends FileFilter {
	private String fileExt;
	private String[] fileExts;
	private boolean type = false;
	private String description;
	private int length;
	private String extension;

	public GenericFile(String[] filesExtsIn, String description) {
		if (filesExtsIn.length == 1) {
			this.type = true;
			this.fileExt = filesExtsIn[0];
		} else {
			this.fileExts = filesExtsIn;
			this.length = this.fileExts.length;
		}
		this.description = description;
	}

	public boolean accept(File f) {
		if (f.isDirectory()) {
			return true;
		}
		this.extension = getExtension(f);
		if (this.extension != null) {
			if (this.type) {
				return check(this.fileExt);
			}

			for (int i = 0; i < this.length; ++i) {
				if (check(this.fileExts[i]))
					return true;
			}
		}

		return false;
	}

	private boolean check(String in) {
		return this.extension.equalsIgnoreCase(in);
	}

	public String getDescription() {
		return this.description;
	}

	private String getExtension(File file) {
		String filename = file.getName();
		int length = filename.length();
		int i = filename.lastIndexOf(46);
		if ((i > 0) && (i < length - 1))
			return filename.substring(i + 1).toLowerCase();
		return null;
	}
}